#include<cstdio>
#include<cstdlib>
char s[101][51];
struct In
{
	int rnum, x;
}a[101];
int m,n;
int cal(char * s)
{
	int res=0;
	for(int i=0;i<m-1;i++)
		for(int j=i+1;j<m;j++)
			if(s[i]>s[j])res++;
	return res;
}
int cmp( const void *a , const void *b ) 
{ 
	struct In *c = (In *)a; 
	struct In *d = (In *)b; 
	if(c->rnum != d->rnum) return c->rnum - d->rnum; 
	else return c->x - d->x; 
} 

int main()
{
	scanf("%d%d",&m,&n);
	a[0].rnum=-1;a[0].x=0;
	for(int i=1;i<=n;i++)
	{
		scanf("%s",s[i]);
		a[i].x=i;
		a[i].rnum=cal(s[i]);
	}
	qsort(a,n+1,sizeof(a[0]),cmp);
	for(int i=1;i<=n;i++)
		printf("%s\n",s[a[i].x]);
	return 0;
}
